//
//  EmoticonViewController.swift
//  02-表情测试
//
//  Created by 唐万超 on 15/8/9.
//  Copyright © 2015年 唐万超. All rights reserved.
//

import UIKit

/// Cell标识符
private let TWCEmoticonCellIdentifier = "TWCEmoticonCellIdentifier"

class EmoticonViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        setupUI()
    }
    
    // toolBar按钮点击方法
    func clickItem(item: UIBarButtonItem) {
        print(item.tag)
    }
    // 设置UI
    private func setupUI() {
    
        // 添加控件
        view.addSubview(collectionView)
        view.addSubview(toolBar)
        // 设置约束
        collectionView.translatesAutoresizingMaskIntoConstraints = false
        toolBar.translatesAutoresizingMaskIntoConstraints = false
        
        let dict = ["cv" : collectionView, "tb" : toolBar]
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[cv]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: dict))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[tb]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: dict))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[cv]-0-[tb(44)]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: dict))
        
        prepareToolbar()
        prepareCollectionView()
        
    }
    
    // 准备collectionView 
    private func prepareCollectionView() {
        
        // 注册可重用的cell
        collectionView.registerClass(EmoticonCell.self, forCellWithReuseIdentifier: TWCEmoticonCellIdentifier)
        // 设置数据源
        collectionView.dataSource = self
    }
    
    // 准备toolBar
    private func prepareToolbar() {
        toolBar.tintColor = UIColor.darkGrayColor()
        var items = [UIBarButtonItem]()
        var index: Int = 0
        for s in ["最近使用", "默认", "emoji", "浪小花"]
        {
            items.append(UIBarButtonItem(title: s, style: UIBarButtonItemStyle.Plain, target: self, action: "clickItem:"))
            items.last?.tag = index++
            items.append(UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil))
            
        }
        
        items.removeLast()
        toolBar.items = items
    }

    // MARK: - 懒加载控件
    // 表情视图
    private lazy var collectionView:UICollectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: TWCCollectionViewLayout())
    // 底部toolBar
    private lazy var toolBar: UIToolbar = UIToolbar()
    
    
    // 控制器中的私有类，类中类，保证类的安全性只在本类中能够使用
    private class TWCCollectionViewLayout: UICollectionViewFlowLayout {
        
        private override func prepareLayout() {
            
            let w = collectionView!.bounds.width / 7.0
            itemSize = CGSize(width: w, height: w)
            scrollDirection = UICollectionViewScrollDirection.Horizontal
            collectionView?.showsHorizontalScrollIndicator = false
            
            // 分页设置
            collectionView?.pagingEnabled = true
            
            minimumInteritemSpacing = 0
            minimumLineSpacing = 0
            
            let y = (collectionView!.bounds.height - 3 * w) * 0.499
            
            sectionInset = UIEdgeInsets(top: y, left: 0, bottom: y, right: 0)
          
        }
        
    }

}

// MARK: - collectionView数据源方法
extension EmoticonViewController: UICollectionViewDataSource {
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 21 * 3
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(TWCEmoticonCellIdentifier, forIndexPath: indexPath) as! EmoticonCell
        
        cell.backgroundColor = (indexPath.item % 2 == 0) ? UIColor.redColor() : UIColor.greenColor()
        return cell
        
    }
    
}

// collectionViewCell
private class EmoticonCell: UICollectionViewCell {

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        emoticonButton.backgroundColor = UIColor.whiteColor()
        emoticonButton.frame = CGRectInset(bounds, 4, 4)
        contentView.addSubview(emoticonButton)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    // 懒加载控件
    private lazy var emoticonButton: UIButton = UIButton()
    
}

